package com.sunday.common.mq.rocket.brave.study.base.e13_MQadmin;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.rocketmq.srvutil.ServerUtil;
import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
import org.apache.rocketmq.tools.command.topic.UpdateTopicSubCommand;
import org.junit.jupiter.api.Test;

/**
 * https://github.com/apache/rocketmq/blob/master/docs/cn/operation.md
 * org.apache.rocketmq.tools.command.MQAdminStartup
 */
@Slf4j
public class RocketMQAdmin {

    DefaultMQAdminExt admin;

//    String namesrvAddr = "172.26.224.1:9876";

    String namesrvAddr = "172.26.224.1:10911";

    String brokerName = "broker-single";

    String topic = "your_topic_name";

//    String consumerGroup = "your_consumer_group_name";

//    @BeforeEach
//    public void start() {
//        try {
//            admin = new DefaultMQAdminExt();
//            // 设置NameServer地址
//            admin.setNamesrvAddr(namesrvAddr);
//            // 启动DefaultMQAdminExt
//            admin.start();
//            log.info("MQAdmin start");
//        } catch (Exception e) {
//            log.error("{}", e.toString(), e);
//        }
//    }

    /**
     * updateTopic	创建更新Topic配置
     * -b	Broker 地址，表示 topic 所在 Broker，只支持单台Broker，地址为ip:port
     * -c	cluster 名称，表示 topic 所在集群（集群可通过 clusterList 查询）
     * -h-	打印帮助
     * -n	NameServer服务地址，格式 ip:port
     * -p	指定新topic的读写权限( W=2|R=4|WR=6 )
     * -r	可读队列数（默认为 8）
     * -w	可写队列数（默认为 8）
     * -t	topic 名称（名称只能使用字符 ^[a-zA-Z0-9_-]+$ ）
     */
    @Test
    public void updateTopic() throws Exception {
        UpdateTopicSubCommand cmd = new UpdateTopicSubCommand();
//        String[] args = new String[]{"-n", "172.26.224.1:9876", "-t", topic,  "-c", "DefaultCluster"};
        String[] args = new String[]{"-b", "172.26.224.1:10911", "-t", topic};

        Options options = ServerUtil.buildCommandlineOptions(new Options());

        CommandLine commandLine = ServerUtil.parseCmdLine("mqadmin " + cmd.commandName(), args, cmd.buildCommandlineOptions(options), new DefaultParser());

        cmd.execute(commandLine, options, null);
    }

//    @AfterEach
//    public void shutdown() {
//        log.info("MQAdmin shutdown");
//        admin.shutdown();
//    }

}

